package com.ruoyi.customerSample.service.impl;

import com.ruoyi.common.utils.uuid.IdUtils;

import com.ruoyi.customerSample.domain.SampleDetailBill;
import com.ruoyi.customerSample.domain.SampleReceptionBill;
import com.ruoyi.customerSample.mapper.SampleDetailBillMapper;
import com.ruoyi.customerSample.mapper.SampleReceptionBillMapper;
import com.ruoyi.customerSample.service.ISampleReceptionBillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;


/**
 * 样品接收单据Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-04-10
 */
@Service
public class SampleReceptionBillServiceImpl implements ISampleReceptionBillService
{
    @Autowired(required = false)
    private SampleReceptionBillMapper sampleReceptionBillMapper;

    @Autowired(required = false)
    private SampleDetailBillMapper sampleDetailBillMapper;

    /**
     * 查询样品接收单据
     * 
     * @param id 样品接收单据ID
     * @return 样品接收单据
     */
    @Override
    public SampleReceptionBill selectSampleReceptionBillById(String id)
    {
        SampleReceptionBill bill = sampleReceptionBillMapper.selectSampleReceptionBillById(id);
        //查看详情信息
        SampleDetailBill sampleDetailBill = new SampleDetailBill();
        sampleDetailBill.setSid(id);
        List<SampleDetailBill> list = sampleDetailBillMapper.selectSampleDetailBillList(sampleDetailBill);
        if(list.size() > 0) {
            bill.setList(list);
            //详情根据类别分组
            Map<String, List<SampleDetailBill>> groupingMap = list.stream()
                    .sorted(Comparator.comparing(SampleDetailBill::getSorts, Comparator.nullsLast(Comparator.reverseOrder())).reversed())
                    .collect(Collectors.groupingBy(SampleDetailBill::getSampleModel, LinkedHashMap::new, Collectors.toList()));
            //遍历map
            if(!groupingMap.isEmpty()) {
                List<List<SampleDetailBill>> modelLists = new ArrayList<>();
                for(Map.Entry<String, List<SampleDetailBill>> map : groupingMap.entrySet()) {
                    modelLists.add(map.getValue());
                }
                bill.setModelList(modelLists);
            }
        }
        return bill;
    }


    /**
     * 根据项目编号获取样品信息
     * @param itemNum
     * @return
     */
    @Override
    public SampleReceptionBill selectSampleReceptionBillByItemNum(String itemNum) {
        return sampleReceptionBillMapper.selectSampleReceptionBillByItemNum(itemNum);
    }

    /**
     * 查询所有来样登记数据
     * @return
     */
    @Override
    public List<Map<String, Object>> selectAllSampleBill(String client) {
        return sampleReceptionBillMapper.selectAllSampleBill(client);
    }

    @Override
    public List<Map<String, Object>> selectAllSampleBillByNum(String itemNum) {
        return sampleReceptionBillMapper.selectAllSampleBillByNum(itemNum);
    }

    /**
     * 查询样品接收单据列表
     * 
     * @param sampleReceptionBill 样品接收单据
     * @return 样品接收单据
     */
    @Override
    public List<SampleReceptionBill> selectSampleReceptionBillList(SampleReceptionBill sampleReceptionBill)
    {
        List<SampleReceptionBill> sampleReceptionBillList = sampleReceptionBillMapper.selectSampleReceptionBillList(sampleReceptionBill);
        if(sampleReceptionBillList.size() > 0) {
            for(SampleReceptionBill bill : sampleReceptionBillList) {
                //查看详情信息
                SampleDetailBill sampleDetailBill = new SampleDetailBill();
                sampleDetailBill.setSid(bill.getId());
                List<SampleDetailBill> list = sampleDetailBillMapper.selectSampleDetailBillList(sampleDetailBill);
                if(list.size() > 0) {
                    bill.setList(list);
                }
            }
        }
        return sampleReceptionBillList;
    }

    /**
     * 新增样品接收单据
     * 
     * @param sampleReceptionBill 样品接收单据
     * @return 结果
     */
    @Override
    public int insertSampleReceptionBill(SampleReceptionBill sampleReceptionBill)
    {
        String id = IdUtils.fastSimpleUUID();
        //增加详情信息
        if(sampleReceptionBill.getList().size() > 0) {
            for(SampleDetailBill detailBill : sampleReceptionBill.getList()) {
                detailBill.setId(IdUtils.fastSimpleUUID());
                detailBill.setSid(id);
            }
            sampleDetailBillMapper.batchSampleDetailBill(sampleReceptionBill.getList());
        }
        sampleReceptionBill.setId(id);
        return sampleReceptionBillMapper.insertSampleReceptionBill(sampleReceptionBill);
    }

    /**
     * 修改样品接收单据
     * 
     * @param sampleReceptionBill 样品接收单据
     * @return 结果
     */
    @Override
    public int updateSampleReceptionBill(SampleReceptionBill sampleReceptionBill)
    {
        int ret = sampleReceptionBillMapper.updateSampleReceptionBill(sampleReceptionBill);
        if(ret > 0) {
            //删除原本信息
            sampleDetailBillMapper.deleteSampleDetailBillBySid(sampleReceptionBill.getId());
            //增加新的数据
            if(sampleReceptionBill.getList().size() > 0) {
                for(SampleDetailBill detailBill : sampleReceptionBill.getList()) {
                    detailBill.setId(IdUtils.fastSimpleUUID());
                    detailBill.setSid(sampleReceptionBill.getId());
                }
                sampleDetailBillMapper.batchSampleDetailBill(sampleReceptionBill.getList());
            }
        }
        return ret;
    }

    /**
     * 根据样品项目编号变更
     * @param sampleReceptionBill 样品接收单据
     * @return
     */
    @Override
    public int updateSampleReceptionBillByNum(SampleReceptionBill sampleReceptionBill) {
        return sampleReceptionBillMapper.updateSampleReceptionBillByNum(sampleReceptionBill);
    }

    /**
     * 批量删除样品接收单据
     * 
     * @param ids 需要删除的样品接收单据ID
     * @return 结果
     */
    @Override
    public int deleteSampleReceptionBillByIds(String[] ids)
    {
        int ret = sampleReceptionBillMapper.deleteSampleReceptionBillByIds(ids);
        if(ret > 0) {
            //删除明细单据
            if(ids.length > 0) {
                for(int i = 0; i < ids.length; i++) {
                    sampleDetailBillMapper.deleteSampleDetailBillBySid(ids[i]);
                }
            }
        }
        return ret;
    }

    /**
     * 删除样品接收单据信息
     * 
     * @param id 样品接收单据ID
     * @return 结果
     */
    @Override
    public int deleteSampleReceptionBillById(String id)
    {
        return sampleReceptionBillMapper.deleteSampleReceptionBillById(id);
    }
}
